const http = require('http');
const fs = require('fs');
const path = require('path');
const { url } = require('inspector');

function getFiles(){
    
}

http.createServer((req,res)=>{
    var urlStr = req.url;
    var filePath = path.join(__dirname,'/index.html');
    var imgPath = path.join(__dirname,'/1.png')
    /**當請求的路徑為/的時候，相應網頁文件到瀏覽器 */
    if(urlStr == '/'){
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200,{'Content-type':"text/html;charset=utf8"});
        res.write(fileContent);
        res.end();
    }
    //获取图片
    else if(urlStr=='/1.png'){
        var imgContent = fs.readFileSync(imgPath);
        res.writeHead(200,{'Content-type':"image/png"});
        res.write(imgContent)
        res.end();
    }
    else if(urlStr=='/getlist'){
        var list = [
            
        ]
        var filesPath = path.join(__dirname,'/fileDir');
        var filesContent = fs.readdirSync(filesPath);
        console.log(fileContent);
        var jsonStr = JSON.stringify(filesContent);
        res.writeHead(200,{'Content-Type':'text/plain'});
        res.write(jsonStr);
        res.end();
    }
}).listen(8082)

console.log('server is listening 8082')